package kuaidi.view.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import kuaidi.entity.zhouweikuaidi.CoListBean;
import kuaidi.helper.R;

/**
 * Created by Jhon on 2016/11/2.
 */

public class FuJinAdapter extends RecyclerView.Adapter<FuJinAdapter.FujinViewHolder> implements View.OnClickListener {
    private List<CoListBean> data;
    private Context context;
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;


    public FuJinAdapter(List<CoListBean> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @Override
    public FujinViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_fujin_layout, null);
        FujinViewHolder fujinViewHolder = new FujinViewHolder(view);
        view.setOnClickListener(this);
        return fujinViewHolder;
    }

    @Override
    public void onBindViewHolder(FujinViewHolder holder, int position) {

        if (data.size() != 0) {
            holder.itemView.setTag(data.get(position));
            Glide.with(context).load(data.get(position).getLogo()).placeholder(R.mipmap.ic_launcher).into(holder.fujinLogoIv);
            holder.fujinCompanyTv.setText(data.get(position).getCompanyName());
            holder.fujinNameTv.setText(data.get(position).getCourierName());
            holder.fujinWorkTv.setText(data.get(position).getWorkArea());
            holder.fujinPriceTv.setText(data.get(position).getPrice()+"");
            holder.fujinTimeTv.setText(data.get(position).getAvgTime()+"");
        }
    }

    @Override
    public int getItemCount() {
        return data != null ? data.size() : 0;
    }

    @Override
    public void onClick(View view) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            mOnItemClickListener.onItemClick(view, (CoListBean) view.getTag());
        }
    }

    public class FujinViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.fujin_logo_iv)
        ImageView fujinLogoIv;
        @BindView(R.id.fujin_name_tv)
        TextView fujinNameTv;
        @BindView(R.id.fujin_company_tv)
        TextView fujinCompanyTv;
        @BindView(R.id.fujin_work_tv)
        TextView fujinWorkTv;
        @BindView(R.id.fujin_price_tv)
        TextView fujinPriceTv;
        @BindView(R.id.fujin_time_tv)
        TextView fujinTimeTv;

        public FujinViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, CoListBean data);
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }


}
